home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13154 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  63 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can you printf a long
  5. Date: 4 Apr 1996 23:11:10 GMT
  6. Organization: OpenVision
  7. Message-ID: <4k1kue$3cp@spanky.pls.ov.com>
  8. References: <4ju8o1$dc3@news.netam.net>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article dc3@news.netam.net, bgc@alpha.netam.net (The Bowling Green Connection) writes:
  13. >I had some trouble with this code:
  14. >(I'm using gcc on Digital Unix)
  15. >
  16. >int main() {
  17. >    long i;
  18. >
  19. >    while(1) {
  20. >    i++;
  21. >    printf("%f\n", i);
  22. >    }
  23. >
  24. >    return 0;
  25. >}
  26. >
  27. >The numbers printed to the screen as 0.00000.
  28. >(Nevermind that this is an infinite loop--I was piping the output to
  29. >a file which made me run out of disk space. This is just for testing)
  30. >
  31. >But when I changed %f to %d, the numbers printed correctly.
  32. >I thought that %d had the same limitations as an int..that is,
  33. >it could only print about 4 bits of information, whereas a long
  34. >is capable of more (8 or 16 bits), so wouldn't %f (float) be able
  35. >to better handle those long numbers?  And why did %d work
  36. >correctly, even up to 634,000 (that's when my disk space ran out.. :))
  37. >
  38. >Well, then I changed the "long i" declaration to "int i", and changed %f 
  39. >to %d, and I got the SAME results!  The numbers went up to 634,000!
  40. >I didn't think an int could handle this much!
  41. >
  42. >Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
  43. >BOTH returned a 4!!!  That can't be right, can it??!
  44. >
  45. >_______________________________bgc@bgcky.com___________________________________
  46. >Edgar E. Easterly, IV   http://www.bgcky.com    Bowling Green, Kentucky   -=O=-
  47. >"Except the Lord build thy house, ye labour in vain that build it; Except   *
  48. >the Lord keep thy city, the watchman will awake but in vain." Psalms 127:1  |
  49.  
  50.  
  51. 1.  The size of a short, int, and long is implementation dependent.
  52.  
  53. 2.  A long is not a float, and if you use %f you are expecting to print a
  54.     float.
  55.  
  56. 3.  To properly print a long use: "%ld".
  57.  
  58. 4.  On your machine, a long can handle 2,147,483,647 before it overflows,
  59.     and an unsigned long can handle 4,294,967,295.
  60.  
  61.             Fletcher.Glenn@ov.com
  62.  
  63.